<script setup lang="ts">
import { showSuccessToast, showToast, type FormInstance } from 'vant'
import { mobileRules, passwordRules, codeRules } from '../../utils/rules'
import { ref } from 'vue'
import { loginByPassword, sendMobileCode, loginByCode } from '@/services/user'
import { useUserStore } from '@/stores'
import { useRouter, useRoute } from 'vue-router'
import { onUnmounted } from 'vue'
const router = useRouter()
const route = useRoute()
const store = useUserStore()

// 定义表单变量
const mobile = ref('13230000077')
const password = ref('abc12345')
// 勾选协议
const agree = ref(false)
// 登录模式切换
const isPass = ref(false)

// ! 手机号密码&验证码登录
const login = async () => {
  if (!agree.value) return showToast('请勾选用户协议')
  // 发送请求
  // ~ 根据isPass的值判断执行手机号密码登录还是短信验证码登录
  const res = isPass.value
    ? await loginByPassword(mobile.value, password.value)
    : await loginByCode(mobile.value, code.value)
  // 存储用户信息
  store.setUser(res.data)
  // 给出提示
  showSuccessToast('登录成功')
  // 跳转页面
  // # replace 与 push 跳转页面的区别
  // replace 跳转到下一页会清除历史记录
  // push 跳转到下一页不会清除历史记录
  router.replace((route.query?.returnUrl as string) || '/user')
}

// ! 获取验证码
const code = ref('')
let timer: number
const time = ref(0)
const form = ref<FormInstance>()
const onSend = async () => {
  // 判断是否是时间大于0，大于0就不能再次发送验证码，防止多次发送
  if (time.value > 0) return
  //验证手机号是否合法
  await form.value?.validate('mobile')
  // 发送请求获取验证码
  await sendMobileCode(mobile.value)
  showToast('发送验证码成功')
  // 设置60s的延时器
  time.value = 60
  if (timer) return clearInterval(timer)
  timer = setInterval(() => {
    if (time.value > 0) {
      time.value--
    } else {
      clearInterval(timer)
    }
  }, 1000)
}
// ! 组件卸载时清除定时器
onUnmounted(() => {
  clearInterval(timer)
})
</script>

<template>
  <div class="login-page">
    <cp-nav-bar
      right-text="注册"
      @click-right="$router.push('/register')"
    ></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;" @click="isPass = !isPass">
        <span>{{ isPass ? '短信验证码登录' : '密码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <!-- ! 表单submit事件会自动的对表单校验，校验通过才会执行回调函数 -->
    <van-form autocomplete="off" @submit="login" ref="form">
      <van-field
        name="mobile"
        v-model="mobile"
        :rules="mobileRules"
        placeholder="请输入手机号"
        type="tel"
      ></van-field>
      <van-field
        v-if="isPass"
        v-model="password"
        :rules="passwordRules"
        placeholder="请输入密码"
        type="password"
      ></van-field>
      <van-field
        v-else
        v-model="code"
        :rules="codeRules"
        placeholder="短信验证码"
      >
        <template #button>
          <span class="btn-send" @click="onSend">{{
            time > 0 ? `${time}s后再次发送` : '发送验证码'
          }}</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit"
          >登 录</van-button
        >
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/qq.svg" alt="" />
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }
  &-head {
    display: flex;
    padding: 30px 30px 50px;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}
.van-form {
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
  .btn-send {
    color: var(--cp-primary);
    &.active {
      color: rgba(22, 194, 163, 0.5);
    }
  }
}
</style>
